using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using System.Text;
using QLVMBAYDomain;

namespace QLVMBAYDatacess
{
    public class LoaiVeDA : DataAccessBase
    {
		#region Constructors
		
		public LoaiVeDA() : base()
        {
			_selectSql = "Select * From LOAIVE";
            _insertSql = "Insert Into LOAIVE(MALOAIVE,TENLOAIVE,PHIHUY,DIENGIAI) Values (@MALOAIVE,@TENLOAIVE,@PHIHUY,@DIENGIAI)";
            _updateSql = "Update LOAIVE Set TENLOAIVE=@TENLOAIVE,PHIHUY=@PHIHUY,DIENGIAI=@DIENGIAI Where 1=1 And MALOAIVE=@MALOAIVE";
        	_deleteSql = "Delete From LOAIVE Where 1=1 And MALOAIVE=@MALOAIVE";
        	_primarSql = "Select * From LOAIVE Where 1=1 And MALOAIVE=@MALOAIVE";
        }
		
		protected static LoaiVeDA _inst = null;
		public static LoaiVeDA Inst
		{
			get
			{
				if(_inst == null)
					_inst = new LoaiVeDA();
				return _inst;
			}
		}
		
		#endregion
		
        #region Get Methods

		private LoaiVe GetCurrent(IDataReader reader, bool isDeep)
		{
			LoaiVe ret = new LoaiVe();
			ret.MaLoaiVe = (string)reader["MALOAIVE"];
			ret.TenLoaiVe = (string)reader["TENLOAIVE"];
           
            ret.PhiHuy = (decimal)reader["PHIHUY"];
            ret.DienGiai = reader["DIENGIAI"] != DBNull.Value ? (string)reader["DIENGIAI"] : "";
			
			
			return ret;
		}

		public List<LoaiVe> GetAll(bool isDeep)
		{
			DbCommand comm = GetCommand(_selectSql);
            using (Connection)
            {
                using (DbDataReader dr = comm.ExecuteReader())
                {
                    List<LoaiVe> ret = new List<LoaiVe>();
					while(dr.Read())
					{
						ret.Add(GetCurrent(dr, isDeep));
					}
					return ret;
                }
            }
		}
		
		public LoaiVe GetByPrimaryKey(bool isDeep, params object[] args)
		{
			DbCommand cmd = GetCommand(_primarSql);
			if(args.Length < 1)
			{
				throw new Exception();
			}
            SetParam(cmd, "@MALOAIVE", args[0]);

            using (Connection)
            {
                using (DbDataReader dr = cmd.ExecuteReader())
                {
					if(dr.Read())
					{
						return GetCurrent(dr, isDeep);
					}
					return null;
                }
            }
		}
		
		public bool Insert(LoaiVe entity)
		{
			DbCommand cmd = GetCommand(_insertSql);
            SetParam(cmd, "@MALOAIVE", entity.MaLoaiVe);
            SetParam(cmd, "@TENLOAIVE", entity.TenLoaiVe);
            
            SetParam(cmd, "@PHIHUY", entity.PhiHuy);
            
            SetParam(cmd, "@DIENGIAI", entity.DienGiai);

			
            using (Connection)
            {
				int ret = cmd.ExecuteNonQuery();
				return ret > 0;
            }
		}
		
		public bool Update(LoaiVe entity)
		{
			DbCommand cmd = GetCommand(_updateSql);
            SetParam(cmd, "@MALOAIVE", entity.MaLoaiVe);
            SetParam(cmd, "@TENLOAIVE", entity.TenLoaiVe);
            
            SetParam(cmd, "@PHIHUY", entity.PhiHuy);
            
            SetParam(cmd, "@DIENGIAI", entity.DienGiai);

			
            using (Connection)
            {
				int ret = cmd.ExecuteNonQuery();
				return ret > 0;
            }
		}
		
		public bool Delete(LoaiVe entity)
		{
			DbCommand cmd = GetCommand(_deleteSql);
            SetParam(cmd, "@MALOAIVE", entity.MaLoaiVe);

            using (Connection)
            {
				int ret = cmd.ExecuteNonQuery();
				return ret > 0;
            }
		}
		
		#endregion
    }
}


